﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using MyATM.Entity;

namespace MyATM.DataAccess
{
    /// <summary>
    /// This class provided to access the data of transactions.
    /// </summary>
    public class TransactionDA
    {
        public int TransactionLogging(Transaction transaction, string cardId)
        {
            string cmd = string.Format("INSERT INTO [Transaction] ( TransType, ATMID, TransTime, Amount, Fee, CardNumber ) " +
                                       "SELECT {0} AS Expr1, {1} AS Expr2, \"{2}\" AS Expr3, {3} AS Expr4, {4} AS Expr5, \"{5}\" " +
                                       "AS Expr6;", transaction.TransactionType, transaction.AtmId, transaction.Date, transaction.Amount, transaction.Price, transaction.CardId);
            int resultLog = SqlAccess.ExcuteNonQuery(cmd);
            string updateCmd = string.Format("UPDATE Account SET Account.Balance = Account.Balance-\"{0}\" " +
                                             "WHERE ((Account.AccountNumber=(SELECT AccountNumber FROM DebitCard " +
                                             "WHERE CardNumber = \"{1}\")));", transaction.Amount, transaction.CardId);
            int resultUpdate = SqlAccess.ExcuteNonQuery(updateCmd);
            return resultUpdate;
        }
    }
}
